package anc.tankshooter.network;

import java.util.ArrayList;

import anc.server.tankshooter.models.Account;
import anc.server.tankshooter.models.Match;

public abstract class GameDataListener implements IPacketReceiver {

	@Override
	public void onConnected() {
		
	}

	@Override
	public void onDisconnected() {
		
	}
	@Override
	final public void onReceive(int command,String data) {
		String[] info;
		switch(command){
		case CommandList.SERVER_SIGNUP_SUCCESS:
		case CommandList.SERVER_SIGNUP_FAILED:
			onSignupResult(command==CommandList.SERVER_SIGNUP_SUCCESS?true:false);
			break;
		case CommandList.SERVER_LOGIN_SUCCESSED:
			info = data.split(":");
			if(info!=null && info.length==5){
				Account account = new Account();
				account.setAccountId(Integer.valueOf(info[0]));
				account.setAvatarId(Integer.valueOf(info[1]));
				account.setName(info[2]);
				account.setEmail(info[3]);
				account.setLevel(Integer.valueOf(info[4]));
				onLoginSuccessed(account);
			}
			break;
		case CommandList.SERVER_LOGIN_FAILED:
			int errorCode = Integer.valueOf(data);
			onLoginFailed(errorCode);
			break;
		case CommandList.SERVER_ALL_ROOM:
			ArrayList<Match> matchList = new ArrayList<Match>();

			String[] rooms = data.split("\\|");
			if(rooms!=null){
				for(String m:rooms){
					info = m.split(":");
					if(info!=null){
						Match match = new Match();
						match.setOwnerId(Integer.valueOf(info[1]));
						match.setOwner(info[2]);
						match.setCompetitorId(Integer.valueOf(info[3]));
						match.setCompetitor(info[4]);
						match.setMapId(Integer.valueOf(info[5]));
						match.setStartTime(Integer.valueOf(info[6]));
						match.setEndTime(Integer.valueOf(info[7]));
						matchList.add(match);
					}
				}
			}
			this.onReceiveRoomList(matchList);
			break;
		case CommandList.SERVER_JOIN_SUCCESSED:
			info = data.split(":");
			if(info!=null){
				int room = Integer.valueOf(info[0]);
				Match match = new Match();
				match.setOwnerId(Integer.valueOf(info[1]));
				match.setOwner(info[2]);
				match.setCompetitorId(Integer.valueOf(info[3]));
				match.setCompetitor(info[4]);
				match.setMapId(Integer.valueOf(info[5]));
				match.setStartTime(Integer.valueOf(info[6]));
				match.setEndTime(Integer.valueOf(info[7]));
				onNewPlayerJoin(room,match);
			}
			break;
		case CommandList.SERVER_JOIN_FAILED:
			int room = Integer.valueOf(data);
			onJoinFaild(room);
			break;
		case CommandList.SERVER_LEAVE_ROOM:
			int accountId = Integer.valueOf(data);
			onLeaveRoom(accountId);
		case CommandList.SERVER_START:
			int map = Integer.valueOf(data);
			onStartGame(map);
			break;
		
		case CommandList.SERVER_DISCONNECTED:
			onServerDisconnected();
			break;
		}
	}
	public abstract void onSignupResult(boolean isSuccess);
	public abstract void onLoginSuccessed(Account acc);
	public abstract void onLoginFailed(int errorCode);
	public abstract void onReceiveRoomList(ArrayList<Match> matchList);
	public abstract void onNewPlayerJoin(int roomIndex,Match m);
	public abstract void onJoinFaild(int room);
	public abstract void onLeaveRoom(int accountId);
	public abstract void onStartGame(int map);
	public abstract void onServerDisconnected();
	
}
